# coding: utf-8
'''
Created on 2009-7-16

@author: roy
'''


import time
from common.httpHepler import *
from constant import *
from common.util import *


class garden:
    '''
    Kaixin garden
    '''


    def __init__(self, me, http, verify):
        '''
        Constructor
        
        me: my uid
        http: httpHelper instance 
        verify: verify code
        '''
        
        self.__http = http
        self.__me = me
        self.__verify = verify
        
    
    def harvest(self):
        '''
        Harvest garden
        
        fList: friend list
        '''
        
        #log('-'*20 + ' garden ' + '-'*20)
        
        # advanced fruits:
#===============================================================================
#        ADVFRUITS = (encode_str('冬虫夏草'), \
#                     encode_str('雪莲'), \
#                     encode_str('灵芝'), \
#                     encode_str('何首乌'), \
#                     encode_str('人形何首乌'), \
#                     encode_str('人参') )
#===============================================================================
        ADVFRUITS = (encode_str('冬虫夏草'), 
                     encode_str('雪莲'), 
                     encode_str('灵芝'), 
                     encode_str('何首乌'), 
                     encode_str('人形何首乌'), 
                     encode_str('人参'),
                     encode_str('珙桐'),
                     encode_str('摇钱树'),
                     encode_str('藏红花'),
                     encode_str('断肠草'),
                     encode_str('绣球花'),
                     encode_str('曼珠沙华'),
                     encode_str('黄曼珠沙华'),
                     encode_str('红曼珠沙华') )

        NOHARVEST= ( encode_str('曼陀罗'),
                    )
        
        # get stealable friends list
        mfList = dict()
        mfList[self.__me] = 'mySelf'
        url = '/house/garden/getfriendmature.php?verify=' + self.__verify
        content = self.__http.read_page(url)
        if (content != None) and ( content != ''): 
            regex = u'<a href=\"/house/garden/\\?[^>]*uid=([^;&]*)[^>]*>([^<]*)</a>[^\u5DF2\u6210\u719F]*\u5DF2\u6210\u719F'
            mlist = search_text(regex, content, True)
            if len(mlist) > 0:
                for i in range(len(mlist)):
                    mfList[mlist[i][0]] = mlist[i][1]
        
        for uid, name in mfList.items():
            # first, get the garden page for each friend
            if self.__me == uid:
                url = '/house/garden/?uid=%s&verify=%s'
                url = url % (uid, self.__verify)
            else:
                url = '/house/garden/?verify=%s&fuid=%s'
                url = url % (self.__verify, uid)
                
            content = self.__http.read_page(url)
            
            if (content == None) or (content == ''):
                continue
            
            # second, replace some spec words:
            content = content.replace(encode_str('aixindi.gif'), u'\u0000')
            #content = content.replace(encode_str('已偷过'), u'\u0001')
            #content = content.replace(encode_str('再过'), u'\u0002')
            #content = content.replace(encode_str('收获'), u'\u0004')
            #content = content.replace(u'aixindi.gif', u'\u0000')            
            content = content.replace(u'\u5DF2\u5077\u8FC7', u'\u0001')
            content = content.replace(u'\u53EF\u8FC7', u'\u0002')
            content = content.replace(u'\u6536\u83B7', u'\u0004')

            
            # third, get harvest list
            #harvestList = dict()
            harvestList = list()
            if self.__me == uid:
                #matchList = search_text(u'<div class=\"list\">\\s*([^\uFF1A]{1,5})\uFF1A\\d+\\s*[^\u0004]*[\u0004][^<]*<input[^>]*name=\"farmnum\"[^>]*value=\"(\\d+?)\"\\s*/>', content, True)
                #matchList = search_text(u'<div class=\"list\">\\s*([^\uFF1A]{1,5})\uFF1A.*farmnum=(\d+?).*>\u0004</a>', content, True)
                matchList = search_text('<a[^>]*href=\"/house/garden/havest.php?[^>]*farmnum=(\\d*)[^>]*>', content, True)
                count = len(matchList)
                if count > 0:
                    for i in range(count):
                        #harvestList[matchList[i][1]] = matchList[i][0]
                        harvestList.append(matchList[i][0])                        
            else:
                #matchList = search_text(u'<div class=\"list\">\\s*([^\uFF1A]{1,5})\uFF1A\\s*<font\\s*color=\"#[0-9A-Za-z]*\">\\s*\u5269\u4F59\\s*(\\d+?)\\s*</font>[^\u0000\u0001\u0002\u5077]*\u5077\"/><input[^>]*name=\"farmnum\"[^>]*value=\"(\\d+?)\"\\s*/>', content, True)
                #matchList = search_text(u'<div class=\"list\">\\s*([^\uFF1A]{1,5})\uFF1A\\s*<font\\s*color=\"#[0-9A-Za-z]*\">\\s*\u5269\u4F59\\s*(\\d+?)\\s*</font><br/><span class="c9">[^\u0000\u0001\u0002\u5077].*farmnum=(\d+?).*\">\u5077</a>', content, True)
                regex = u'<div class=\"list\">\\s*([^\uFF1A]{1,10})\uFF1A\\s*<font\\s*color=\"#[0-9A-Za-z]*\">\\s*\u5269\u4F59\\s*(\\d*)\\s*</font>[^\u0000\u0001\u0002\u5077]*<a[^>]*href=\"/house/garden/havest.php?[^>]*farmnum=(\\d*)[^>]*>\u5077'
                #matchList = search_text(u'<div class=\"list\">\\s*([^\uFF1A]{1,5})\uFF1A\\s*<font\\s*color=\"#[0-9A-Za-z]*\">\\s*\u5269\u4F59\\s*(\\d+?)\\s*</font><br/><span class="c9">[^\u0000\u0001\u0002\u5077].*farmnum=(\d+?).*\">\u5077</a>', content, True)
                matchList = search_text(regex, content, True)
                count = len(matchList)
                if count > 0:
                    for i in range(count):
                        if ((matchList[i][0] in ADVFRUITS) and (matchList[i][1]<='1')) or (matchList[i][0] in NOHARVEST):
                            continue
                        else:
                            #harvestList[matchList[i][2]] = matchList[i][0]
                            harvestList.append(matchList[i][2])

            # harvest  
            #print harvestList
            count = len(harvestList)
            if count > 0:
                #for key, value in harvestList.items():
                for i in range(count):
                    harvestUrl = '/house/garden/havest.php?farmnum=%s&verify=%s&fuid=%s'
                    #harvestUrl %= (key, self.__verify, uid)
                    harvestUrl %= (harvestList[i], self.__verify, uid)
                    content = self.__http.read_page(harvestUrl)
                    value = ''
                    if (content != None) or (content != ''):
                        match = search_text('<div class=\"tips1\">([^<]*)</div>', content, True)
                        if len(match)>0:
                            value = match[0][0]
                    #print content
                    log('Harvest %s, %s' % (name, value))
                    
            #time.sleep(1)
            
        #log('-'*18 + ' end garden ' + '-'*18)
        

def test():
    pass
    
    
if __name__ == '__main__':
    test()     
        
        